package org.edu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.interceptor.RequestAware;
import org.edu.model.User;
import org.edu.service.PurviewTreeNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
/**
 * <h2>权限树Action</h2>
 * <p>已在方法上注明方法作用</p>
 * @author 谢圣强
 * @version 2.0
 */
@Controller
@ParentPackage("struts-default")
@Namespace("/emp")
public class PurviewTreeNodeAction extends ActionSupport implements RequestAware  {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3017020119710058969L;
	private static final Logger logger = Logger.getLogger(PurviewTreeNodeAction.class);
	private Map<String, Object> request;
	private String id;
	private String roleid;
	@Autowired
	private PurviewTreeNodeService purviewTreeNodeService;

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	/**
	 * 获取权限树
	 */
	@Action(value="loadpurviewTree")
	public void loadpurviewTree(){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();

			logger.info("开始加载权限树");
			out.println(purviewTreeNodeService.loadlist(roleid).toString());
		} catch (IOException e) {
			logger.info("获取输出流失败");
		}catch (Exception e) {
			logger.info("加载权限树失败");
		}
		out.flush();
		out.close();
	}
	
	@Action(value="loaduserpurview")
	public void loaduserpurview(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpSession session =  ServletActionContext.getRequest().getSession();
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			User u= (User)session.getAttribute("user");
			logger.info("开始加载"+u.getAccount()+"权限树");
		out.println(purviewTreeNodeService.loaduserpurview(u.getAccount()));
		} catch (IOException e) {
			logger.info("获取输出流失败");
		}catch (Exception e) {
			e.printStackTrace();
			logger.info("加载权限树失败");
		}
		out.flush();
		out.close();
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getRoleid() {
		return roleid;
	}

	public void setRoleid(String roleid) {
		this.roleid = roleid;
	}
	
	
	

}
